package com.erp.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.order.domain.bo.ErpSellerChildOrderExtendBo;
import com.erp.order.domain.vo.ErpSellerChildOrderExtendVo;
import com.erp.order.mapper.ErpSellerChildOrderExtendMapper;
import com.erp.order.service.IErpSellerChildOrderExtendService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.order.api.domain.ErpSellerChildOrderExtend;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 卖家子订单延伸Service业务层处理
 *
 * @author erp
 * @date 2023-06-19
 */
@RequiredArgsConstructor
@Service
public class ErpSellerChildOrderExtendServiceImpl implements IErpSellerChildOrderExtendService {

    private final ErpSellerChildOrderExtendMapper baseMapper;

    @Override
    public List<ErpSellerChildOrderExtend> getChildExtendByOrderId(Long orderId) {
        return baseMapper.selectList(new LambdaQueryWrapper<ErpSellerChildOrderExtend>().eq(ErpSellerChildOrderExtend::getOrderId,orderId));
    }

    /**
     * 查询卖家子订单延伸
     */
    @Override
    public ErpSellerChildOrderExtendVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询卖家子订单延伸列表
     */
    @Override
    public TableDataInfo<ErpSellerChildOrderExtendVo> queryPageList(ErpSellerChildOrderExtendBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerChildOrderExtend> lqw = buildQueryWrapper(bo);
        Page<ErpSellerChildOrderExtendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询卖家子订单延伸列表
     */
    @Override
    public List<ErpSellerChildOrderExtendVo> queryList(ErpSellerChildOrderExtendBo bo) {
        LambdaQueryWrapper<ErpSellerChildOrderExtend> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerChildOrderExtend> buildQueryWrapper(ErpSellerChildOrderExtendBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerChildOrderExtend> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChildOrderId() != null, ErpSellerChildOrderExtend::getChildOrderId, bo.getChildOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReturnFreight()), ErpSellerChildOrderExtend::getIsReturnFreight, bo.getIsReturnFreight());
        lqw.eq(StringUtils.isNotBlank(bo.getReturnFreight()), ErpSellerChildOrderExtend::getReturnFreight, bo.getReturnFreight());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecialCause()), ErpSellerChildOrderExtend::getSpecialCause, bo.getSpecialCause());
        lqw.eq(StringUtils.isNotBlank(bo.getConsignmentNo()), ErpSellerChildOrderExtend::getConsignmentNo, bo.getConsignmentNo());
        lqw.eq(StringUtils.isNotBlank(bo.getProof()), ErpSellerChildOrderExtend::getProof, bo.getProof());
        lqw.eq(bo.getType() != null, ErpSellerChildOrderExtend::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseIds()), ErpSellerChildOrderExtend::getWarehouseIds, bo.getWarehouseIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerChildOrderExtend::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增卖家子订单延伸
     */
    @Override
    public Boolean insertByBo(ErpSellerChildOrderExtendBo bo) {
        ErpSellerChildOrderExtend add = BeanUtil.toBean(bo, ErpSellerChildOrderExtend.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改卖家子订单延伸
     */
    @Override
    public Boolean updateByBo(ErpSellerChildOrderExtendBo bo) {
        ErpSellerChildOrderExtend update = BeanUtil.toBean(bo, ErpSellerChildOrderExtend.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerChildOrderExtend entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除卖家子订单延伸
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> printed(String childOrderId) {
        ErpSellerChildOrderExtend childOrderExtend = baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerChildOrderExtend>().eq(ErpSellerChildOrderExtend::getChildOrderId, Long.parseLong(childOrderId)));
        if (childOrderExtend == null) {
            return R.fail("找不到此订单");
        }
        childOrderExtend.setPrinted("1");
        int update = baseMapper.updateById(childOrderExtend);
        if (update == 1) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public ErpSellerChildOrderExtend getChildExtendByChildOrder(String childOrderId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerChildOrderExtend>().eq(ErpSellerChildOrderExtend::getChildOrderId, childOrderId));
    }
}
